package com.chinasoftosg.sicap.utils.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class PageTag extends TagSupport
{
    
    private PageResult pageResult = new PageResult();
    
    private String formId;
    
    private String className;
    
    private String isNoScript = "false";
    
    public String getFormId()
    {
        return formId;
    }
    
    public void setFormId(String formId)
    {
        this.formId = formId;
    }
    
    public PageResult getPageResult()
    {
        return pageResult;
    }
    
    public void setPageResult(PageResult pageResult)
    {
        try
        {
            this.pageResult = (PageResult)pageResult;
        }
        catch (Exception e)
        {
            this.pageResult = null;
        }
        
    }
    
    public String getClassName()
    {
        return className;
    }
    
    public void setClassName(String className)
    {
        this.className = className;
    }
    
    public String getIsNoScript()
    {
        return isNoScript;
    }
    
    public void setIsNoScript(String isNoScript)
    {
        this.isNoScript = isNoScript;
    }
    
    @Override
    public int doEndTag() throws JspException
    {
        
        StringBuffer buffer = new StringBuffer();
        this.getPageResult();
        PageResult pageResult = this.getPageResult();
        if (null != this.pageResult)
        {
            if (pageResult.getTotalPage() > 1)
            {
                // 加载样式表
                //                buffer.append("<style>\r\n");
                //                buffer.append("#divPage a{color: #333;font-weight: bold;}");
                //                buffer.append("#divPage a:hover{color: #FFFFFF;font-weight: bold;background-color:#999; }");
                //                buffer.append("#divPage span{color: #FFFFFF;font-weight: bold;background-color:#999;margin: 0 5 0 5; }");
                //                buffer.append("</style>\r\n");
                // 加载JS实现方法
                if ("true".equals(this.isNoScript))
                {
                    buffer.append("<input id=\"current\" type=\"hidden\" name=\"pageResult.current\" value=\""
                            + pageResult.getCurrent() + "\"/>");
                    buffer.append("<script type=\"text/javascript\">\r\n");
                    buffer.append("function doSubmit(page,isPre){\r\n if(page==0 || page=="
                            + pageResult.getCurrent()
                            + "){}else{ document.getElementById('current').value=page;"
                            + this.formId + ".submit();}}\r\n");
                    
                    buffer.append("function checkNum(txt){\r\n");
                    buffer.append("    var reg = /^\\d+$/;\r\n");
                    buffer.append("    if(reg.test(txt)){\r\n");
                    buffer.append("        doSubmit(txt);\r\n");
                    buffer.append("    }else{alert('请输入正确的整数！')}\r\n");
                    buffer.append("}\r\n");
                    
                    buffer.append("</script>\r\n");
                }
                buffer.append("<div id=\"divPage\" style=\"display: inline;\" class=\""
                        + this.className + "\" >\r\n");
                //上一页
                if (pageResult.getCurrent() > 1)
                {
                    buffer.append("<a href=\"javascript:doSubmit("
                            + (pageResult.isPre() ? pageResult.getCurrent() - 1
                                    : 0) + ",true);\" >上一页</a>\r\n");
                }
                else
                {
                    buffer.append("<span>上一页</span>\r\n");
                }
                
                // // 加载前五页按钮
                for (int i = ((pageResult.getCurrent() - 5) <= 1 ? 1
                        : (pageResult.getCurrent() - 5)); i < pageResult.getCurrent(); i++)
                {
                    buffer.append("<a href=\"javascript:doSubmit('" + i
                            + "');\" >" + i + "</a>\r\n");
                }
                // 加载后五页按钮
                for (int i = pageResult.getCurrent(); i < pageResult.getCurrent() + 5
                        && i <= pageResult.getTotalPage(); i++)
                {
                    if (i == pageResult.getCurrent())
                    {
                        buffer.append("<span>" + i + "</span>\r\n");
                    }
                    else
                    {
                        buffer.append("<a href=\"javascript:doSubmit('" + i
                                + "');\" >" + i + "</a>\r\n");
                    }
                }
                if (pageResult.getCurrent() != pageResult.getTotalPage())
                {
                    buffer.append("<a href=\"javascript:doSubmit('"
                            + (pageResult.isNext() ? pageResult.getCurrent() + 1
                                    : 0) + "');\" >下一页</a>\r\n");
                }
                else
                {
                    buffer.append("<span>下一页</span>\r\n");
                }
                
                buffer.append("<input id=\"toPage\" style=\"width:27px\" type=\"text\" name=\"toPage\" value=\""+pageResult.getCurrent()+"\" onkeydown=\"if(event.keyCode==13) {checkNum(this.value);}\" onfocus=\"this.select();\" onclick=\"this.select()\" />");
                
                buffer.append("[共：<font>" + pageResult.getTotalRecord()
                        + "</font>条|<font>" + pageResult.getPageSize()
                        + "</font>条/页|共<font>" + pageResult.getTotalPage()
                        + "</font>页]");
                buffer.append("</div>");
            }
        }
        else
        {
            buffer.append("传入分页对象出错！");
        }
        try
        {
            this.pageContext.getOut().println(buffer);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return this.SKIP_BODY;
    }
    
}
